package deng.qiya.util;

import java.io.InputStream;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import deng.qiya.model.Article;
import deng.qiya.model.WeChatLocationMessage;
import deng.qiya.model.WeChatNewsMessage;
import deng.qiya.model.WeChatTextMessage;

public class XStreamMessageUtil {

	public static WeChatTextMessage getWeChatTextMessage(InputStream xmlStream){
		
		XStream xstream = new XStream(new DomDriver());
		xstream.alias("xml", WeChatTextMessage.class);
		xstream.aliasField("ToUserName", WeChatTextMessage.class, "toUserName");
		xstream.aliasField("FromUserName", WeChatTextMessage.class, "fromUserName");
		xstream.aliasField("CreateTime", WeChatTextMessage.class, "createTime");
		xstream.aliasField("MsgType", WeChatTextMessage.class, "messageType");
		xstream.aliasField("Content", WeChatTextMessage.class, "content");
		xstream.aliasField("MsgId", WeChatTextMessage.class, "msgId");
		WeChatTextMessage wechatTextMessage = (WeChatTextMessage)xstream.fromXML(xmlStream); 
		return wechatTextMessage;
	}
	
	public static WeChatTextMessage getWeChatTextMessage(String xml){
		
		XStream xstream = new XStream(new DomDriver());
		xstream.alias("xml", WeChatTextMessage.class);
		xstream.aliasField("ToUserName", WeChatTextMessage.class, "toUserName");
		xstream.aliasField("FromUserName", WeChatTextMessage.class, "fromUserName");
		xstream.aliasField("CreateTime", WeChatTextMessage.class, "createTime");
		xstream.aliasField("MsgType", WeChatTextMessage.class, "messageType");
		xstream.aliasField("Content", WeChatTextMessage.class, "content");
		xstream.aliasField("MsgId", WeChatTextMessage.class, "msgId");
		WeChatTextMessage wechatTextMessage = (WeChatTextMessage)xstream.fromXML(xml); 
		return wechatTextMessage;
	}
	
	
	public static WeChatLocationMessage getWeChatLocationMessage(InputStream xmlStream){
		XStream xstream = new XStream(new DomDriver());
		WeChatLocationMessage  message = null;
		xstream.alias("xml", WeChatLocationMessage.class);
		xstream.aliasField("ToUserName", WeChatLocationMessage.class, "toUserName");
		xstream.aliasField("FromUserName", WeChatLocationMessage.class, "fromUserName");
		xstream.aliasField("CreateTime", WeChatLocationMessage.class, "createTime");
		xstream.aliasField("MsgType", WeChatLocationMessage.class, "msgType");
		xstream.aliasField("Location_X", WeChatLocationMessage.class, "locationx");
		xstream.aliasField("Location_Y", WeChatLocationMessage.class, "localtiony");
		xstream.aliasField("Scale", WeChatLocationMessage.class, "scale");
		xstream.aliasField("Label", WeChatLocationMessage.class, "label");
		message = (WeChatLocationMessage)xstream.fromXML(xmlStream);
		return message;
	}
	
	public static WeChatLocationMessage getWeChatLocationMessage(String xml){
		XStream xstream = new XStream(new DomDriver());
		WeChatLocationMessage  message = null;
		xstream.alias("xml", WeChatLocationMessage.class);
		xstream.aliasField("ToUserName", WeChatLocationMessage.class, "toUserName");
		xstream.aliasField("FromUserName", WeChatLocationMessage.class, "fromUserName");
		xstream.aliasField("CreateTime", WeChatLocationMessage.class, "createTime");
		xstream.aliasField("MsgType", WeChatLocationMessage.class, "msgType");
		xstream.aliasField("Location_X", WeChatLocationMessage.class, "locationx");
		xstream.aliasField("Location_Y", WeChatLocationMessage.class, "localtiony");
		xstream.aliasField("Scale", WeChatLocationMessage.class, "scale");
		xstream.aliasField("Label", WeChatLocationMessage.class, "label");
		xstream.aliasField("MsgId", WeChatLocationMessage.class, "msgId");
		message = (WeChatLocationMessage)xstream.fromXML(xml);
		return message;
	}
	
	public static String getWeChatNewsMessage(WeChatNewsMessage newsMessage){
		XStream xstream = new XStream(new DomDriver());
		xstream.alias("xml", WeChatNewsMessage.class);
		xstream.aliasField("ToUserName", WeChatNewsMessage.class, "toUserName");
		xstream.aliasField("FromUserName", WeChatNewsMessage.class, "fromUserName");
		xstream.aliasField("CreateTime", WeChatNewsMessage.class, "createTime");
		xstream.aliasField("MsgType", WeChatNewsMessage.class, "msgType");
		xstream.aliasField("ArticleCount", WeChatNewsMessage.class, "articleCount");
		xstream.aliasField("Content", WeChatNewsMessage.class, "content");
		xstream.aliasField("FuncFlag", WeChatNewsMessage.class, "funcFlag");
		xstream.aliasField("Articles", WeChatNewsMessage.class, "articles");
		
		xstream.alias("item", Article.class);
		xstream.aliasField("Title", Article.class, "title");
		xstream.aliasField("Description", Article.class, "description");
		xstream.aliasField("PicUrl", Article.class, "picUrl");
		xstream.aliasField("Url", Article.class, "url");
		
		return xstream.toXML(newsMessage);
	}
	
	public static WeChatNewsMessage getWeChatNewsMessage(String xml){
		WeChatNewsMessage message = new WeChatNewsMessage();
		XStream xstream = new XStream(new DomDriver());
		xstream.alias("xml", WeChatNewsMessage.class);
		xstream.aliasField("ToUserName", WeChatNewsMessage.class, "toUserName");
		xstream.aliasField("FromUserName", WeChatNewsMessage.class, "fromUserName");
		xstream.aliasField("CreateTime", WeChatNewsMessage.class, "createTime");
		xstream.aliasField("MsgType", WeChatNewsMessage.class, "msgType");
		xstream.aliasField("ArticleCount", WeChatNewsMessage.class, "articleCount");
		xstream.aliasField("Content", WeChatNewsMessage.class, "content");
		xstream.aliasField("FuncFlag", WeChatNewsMessage.class, "funcFlag");
		xstream.aliasField("Articles", WeChatNewsMessage.class, "articles");
		
		xstream.alias("item", Article.class);
		xstream.aliasField("Title", Article.class, "title");
		xstream.aliasField("Description", Article.class, "description");
		xstream.aliasField("PicUrl", Article.class, "picUrl");
		xstream.aliasField("Url", Article.class, "url");
		message=(WeChatNewsMessage)xstream.fromXML(xml);
		return message;
	}
	
	
}
